package com.ruoyi.basebill.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.DTO.GjjjrEntryInfo;
import com.ruoyi.basebill.DTO.GjjjrInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.WfzGjjjrentry;
import com.ruoyi.basebill.mapper.WfzGjjjrMapper;
import com.ruoyi.basebill.domain.WfzGjjjr;
import com.ruoyi.basebill.service.IWfzGjjjrService;

/**
 * 公积金缴纳Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-14
 */
@Service
public class WfzGjjjrServiceImpl implements IWfzGjjjrService 
{
    @Autowired
    private WfzGjjjrMapper wfzGjjjrMapper;

    /**
     * 查询公积金缴纳
     * 
     * @param fid 公积金缴纳主键
     * @return 公积金缴纳
     */
    @Override
    public WfzGjjjr selectWfzGjjjrByFid(Long fid)
    {
        return wfzGjjjrMapper.selectWfzGjjjrByFid(fid);
    }

    /**
     * 查询公积金缴纳列表
     * 
     * @param wfzGjjjr 公积金缴纳
     * @return 公积金缴纳
     */
    @Override
    public List<WfzGjjjr> selectWfzGjjjrList(WfzGjjjr wfzGjjjr)
    {
        return wfzGjjjrMapper.selectWfzGjjjrList(wfzGjjjr);
    }

    /**
     * 新增公积金缴纳
     * 
     * @param wfzGjjjr 公积金缴纳
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfzGjjjr(WfzGjjjr wfzGjjjr)
    {
        int rows = wfzGjjjrMapper.insertWfzGjjjr(wfzGjjjr);
        insertWfzGjjjrentry(wfzGjjjr);
        return rows;
    }

    /**
     * 修改公积金缴纳
     * 
     * @param wfzGjjjr 公积金缴纳
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfzGjjjr(WfzGjjjr wfzGjjjr)
    {
        wfzGjjjrMapper.deleteWfzGjjjrentryByFparentid(wfzGjjjr.getFid());
        insertWfzGjjjrentry(wfzGjjjr);
        return wfzGjjjrMapper.updateWfzGjjjr(wfzGjjjr);
    }

    /**
     * 批量删除公积金缴纳
     * 
     * @param fids 需要删除的公积金缴纳主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzGjjjrByFids(Long[] fids)
    {
        wfzGjjjrMapper.deleteWfzGjjjrentryByFparentids(fids);
        return wfzGjjjrMapper.deleteWfzGjjjrByFids(fids);
    }

    /**
     * 删除公积金缴纳信息
     * 
     * @param fid 公积金缴纳主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzGjjjrByFid(Long fid)
    {
        wfzGjjjrMapper.deleteWfzGjjjrentryByFparentid(fid);
        return wfzGjjjrMapper.deleteWfzGjjjrByFid(fid);
    }

    /**
     * 新增公积金缴纳分录信息
     * 
     * @param wfzGjjjr 公积金缴纳对象
     */
    public void insertWfzGjjjrentry(WfzGjjjr wfzGjjjr)
    {
        List<WfzGjjjrentry> wfzGjjjrentryList = wfzGjjjr.getWfzGjjjrentryList();
        Long fid = wfzGjjjr.getFid();
        if (StringUtils.isNotNull(wfzGjjjrentryList))
        {
            List<WfzGjjjrentry> list = new ArrayList<WfzGjjjrentry>();
            for (WfzGjjjrentry wfzGjjjrentry : wfzGjjjrentryList)
            {
                wfzGjjjrentry.setFparentid(fid);
                list.add(wfzGjjjrentry);
            }
            if (list.size() > 0)
            {
                wfzGjjjrMapper.batchWfzGjjjrentry(list);
            }
        }
    }

    /**
     * 导入公积金缴纳
     */
    @Override
    public String importGjjjr(WfzGjjjr wfzGjjjr) {
        GjjjrInfo gjjjrInfo = new GjjjrInfo();
        String fnumber = wfzGjjjr.getFnumber();//单据编号
        String fcompany = wfzGjjjr.getFcompany();//公司（组织）
        String fperiod = wfzGjjjr.getFperiod();//薪酬期间
        BigDecimal fzrs = wfzGjjjr.getFzrs();//总人数
        BigDecimal fnscbzxrs = wfzGjjjr.getFnscbzxrs();//合并纳税成本中心人数
        BigDecimal fglf = wfzGjjjr.getFglf();//关联方人数
        String fvourtype = wfzGjjjr.getFvourtype();//凭证字

        gjjjrInfo.setNumber(fnumber);
        gjjjrInfo.setCompany(fcompany);
        gjjjrInfo.setPeriod(fperiod);
        gjjjrInfo.setZrs(fzrs);
        gjjjrInfo.setNscbzxrs(fnscbzxrs);
        gjjjrInfo.setGlf(fglf);
        gjjjrInfo.setVourType(fvourtype);
        gjjjrInfo.setSourcebillid(String.valueOf(wfzGjjjr.getFid()));

        List<WfzGjjjrentry> wfzGjjjrentryList = wfzGjjjr.getWfzGjjjrentryList();
        ArrayList<GjjjrEntryInfo> entry = new ArrayList<>();
        //组装分录信息
        for (int i = 0; i < wfzGjjjrentryList.size(); i++) {
            WfzGjjjrentry wfzGjjjrentry = wfzGjjjrentryList.get(i);
            String fcostnumber = wfzGjjjrentry.getFcostnumber();//成本中心
            String fcostcompany = wfzGjjjrentry.getFcostcompany();//成本中心所属财务组织
            BigDecimal fgjj = wfzGjjjrentry.getFgjj();//公积金
            BigDecimal fgrgjj = wfzGjjjrentry.getFgrgjj();//个人公积金
            String fjsfs = wfzGjjjrentry.getFjsfs();//组织间结算方式
            String fcbzxxz = wfzGjjjrentry.getFcbzxxz();//成本中心属性
            Long fisdy = wfzGjjjrentry.getFisdy();//是否当月缴纳
            Long fislz = wfzGjjjrentry.getFislz();//是否离职员工
            String fvourtype1 = wfzGjjjrentry.getFvourtype();//凭证字

            GjjjrEntryInfo gjjjrEntryInfo = new GjjjrEntryInfo();
            gjjjrEntryInfo.setCostCenter(fcostnumber);
            gjjjrEntryInfo.setCostcompany(fcostcompany);
            gjjjrEntryInfo.setGjj(fgjj);
            gjjjrEntryInfo.setGrgjj(fgrgjj);
            gjjjrEntryInfo.setJsfs(fjsfs);
            gjjjrEntryInfo.setCbzxxz(fcbzxxz);
            gjjjrEntryInfo.setIsdy(fisdy);
            gjjjrEntryInfo.setIslz(fislz);
            gjjjrEntryInfo.setVourType(fvourtype1);

            entry.add(gjjjrEntryInfo);
        }
        gjjjrInfo.setEntry(entry);

        //转为JSon
        String json = JSONObject.toJSONString(gjjjrInfo);
        return json;
    }
}
